package com.mingqi.enetconnected.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.mingqi.enetconnected.R;
import com.mingqi.enetconnected.view.Dialog.single.SinglePickerAdapter;

import java.util.List;

public class SingleChoiceLayout extends LinearLayout {

    private TextView note;
    private ListView listView;
    private SinglePickerAdapter adapter;
    private String[] textChoiceValue;

    public SingleChoiceLayout(Context context) {
        this(context, null, 0);
    }

    public SingleChoiceLayout(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SingleChoiceLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        LayoutInflater.from(context).inflate(R.layout.view_singlechoice_layout, this);

        note = findViewById(R.id.note);
        listView = findViewById(R.id.lv_single);

        TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs, R.styleable.HorizontalLayout, defStyleAttr, 0);
        int indexCount = typedArray.getIndexCount();
        for (int i = 0; i < indexCount; i++) {
            int index = typedArray.getIndex(i);
            if (index == R.styleable.HorizontalLayout_textSingleChoiceValue) {
                textChoiceValue = getResources().getStringArray(typedArray.getResourceId(index, 0));
            }
        }
        typedArray.recycle();
        initData();

    }

    private void initData() {
        adapter = new SinglePickerAdapter(textChoiceValue);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (position == adapter.getCount() - 1) {
                    note.setEnabled(true);
                } else {
                    note.setText("");
                    note.setEnabled(false);
                }
            }
        });

    }

    public void setChoiceData(List<String> data) {
        adapter.refresh(data);
    }

    public int getCheckedPosition() {
        return listView.getCheckedItemPosition();
    }

    public String getCheckedValue() {
        if (adapter != null && listView.getCheckedItemPosition() != AbsListView.INVALID_POSITION) {

            if (listView.getCheckedItemPosition() == adapter.getCount() - 1) {
                return (String) adapter.getItem(listView.getCheckedItemPosition()) + "-" + note.getText().toString();
            }
            return (String) adapter.getItem(listView.getCheckedItemPosition());
        }
        return null;
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
    }


}
